package com.lyu.xiaolvshu.auth.rpc;

import com.lyu.framework.common.response.Response;
import com.lyu.xiaolvshu.user.api.UserFeignApi;
import com.lyu.xiaolvshu.user.dto.req.FindUserByPhoneReqDTO;
import com.lyu.xiaolvshu.user.dto.req.RegisterUserReqDTO;
import com.lyu.xiaolvshu.user.dto.req.UpdateUserPasswordReqDTO;
import com.lyu.xiaolvshu.user.dto.resp.FindUserByPhoneRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class UserRpcService {

    @Resource
    private UserFeignApi userFeignApi;

    public Long registerUser(String phone) {
        RegisterUserReqDTO registerUserReqDTO = new RegisterUserReqDTO();
        registerUserReqDTO.setPhone(phone);

        Response<Long> response = userFeignApi.register(registerUserReqDTO);

        if (Objects.isNull(response) || !response.isSuccess()) {
            return null;
        }
        return response.getData();
    }

    public FindUserByPhoneRespDTO findUserByPhone(String phone) {
        FindUserByPhoneReqDTO findUserByPhoneReqDTO = FindUserByPhoneReqDTO.builder()
                .phone(phone)
                .build();

        Response<FindUserByPhoneRespDTO> response = userFeignApi.findUserByPhone(findUserByPhoneReqDTO);
        if (Objects.isNull(response) || !response.isSuccess()) {
            return null;
        }
        return response.getData();
    }

    public boolean updatePassword(String encodePassword) {
        UpdateUserPasswordReqDTO updateUserPasswordReqDTO = UpdateUserPasswordReqDTO.builder()
                .encodePassword(encodePassword)
                .build();

        Response<?> response = userFeignApi.updatePassword(updateUserPasswordReqDTO);
        if (Objects.isNull(response)) {
            return false;
        }
        return response.isSuccess();
    }
}
